//
//  OrderDetailViewController.swift
//  BookStore2.0
//
//  Created by hw on 2022/6/2.
//

import UIKit

class OrderDetailViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

    
    @IBOutlet weak var OrderID: UILabel!
    
    @IBOutlet weak var OrderTime: UILabel!
    
    @IBOutlet weak var BuyNum: UILabel!
    
    @IBOutlet weak var TotalPrice: UILabel!
    
    @IBOutlet weak var detailtableView: UITableView!
    
    var orderid:Int?
    var bookid:String?
    var booknum:Int?
    var totalprice:Float?
    var ordertime:String?
    var items:[[String:AnyObject]]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let xib=UINib(nibName: "OrderDetailTableViewCell", bundle: nil)
        detailtableView.register(xib, forCellReuseIdentifier: "orderdetailcell")
        detailtableView.rowHeight=130
        OrderID.text="\(orderid!)"
        TotalPrice.text="\(totalprice!)"
        OrderTime.text="\(ordertime!)"
        BuyNum.text="\(booknum!)"
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        items=DB1.selectOrderItemByOrderID(orderID:orderid!)
        detailtableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if DB1.selectOrderItemByOrderID(orderID:orderid!) != nil {
            return DB1.selectOrderItemByOrderID(orderID:orderid!)!.count
        }
        else {
            return 0
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "orderdetailcell", for: indexPath) as! OrderDetailTableViewCell
        //配置cell
        cell.buynum.text="\((items![indexPath.row]["amount"] as! NSNumber).intValue)"
        cell.eachprice.text="\((items![indexPath.row]["cost"] as! NSNumber).floatValue)"
        cell.bookname.text=items![indexPath.row]["title"] as? String
        let bookid=items![indexPath.row]["bookid"] as! String
        cell.bookimg.image=DB1.imgSet[bookid]
        return cell
    }
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        bookid=items![indexPath.row]["bookid"] as? String
        performSegue(withIdentifier: "tobookdetail2", sender: self)
       }
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nextVC=segue.destination as! BookDetailViewController
        nextVC.ID=bookid
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
}
